package gw.com.android.presenter.http;

import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import gw.com.android.model.ConfigUtil;
import gw.com.android.utils.CacheUtils;
import okhttp3.Request;
import okhttp3.Response;
import www.com.library.interfaces.ReqCallBack;

/**
 * @author jett
 * @since 2017-12-04.
 */
public class FMRequest {

    private String cacheFileName = "_fm_news2.dat";

    public DetailData fetchLocal() {
        String str = CacheUtils.readFile(cacheFileName);
        return parseDetail(str);
    }

    public void call(final ReqCallBack<DetailData> callBack) {
        String url = ConfigUtil.instance().getUrlPath("FMDetail");
        new Https(url).get(new Https.ResponseCallback<String>() {
            @Override
            public void onSuccess(Request request, Response response, String result, int code) {
                DetailData data = parseDetail(result);
                if (callBack != null) {
                    callBack.onReqSuccess(data);
                }
            }

            @Override
            public void onFailure(Request request, IOException e) {
                if (callBack != null) {
                    String msg = e == null ? "" : e.getMessage();
                    callBack.onReqFailed(msg);
                }
            }
        });
    }

    private DetailData parseDetail(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(str);
            JSONObject dataJson = jsonObject.optJSONObject("data");
            if (dataJson != null) {
                DetailData detail = new DetailData();
                detail.id = dataJson.optInt("id", -1);
                detail.createTime = dataJson.optString("pubdate");
                detail.title = dataJson.optString("title");
                detail.content = htmlFormat(dataJson.optString("content"));
                detail.tag = dataJson.optString("tag");
                detail.audioUrl = dataJson.optString("music");
                if ("null".equals(detail.audioUrl)) {
                    detail.audioUrl = null;
                }
                CacheUtils.writeFile(cacheFileName, str);
                return detail;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String htmlFormat(String text) {
        if (TextUtils.isEmpty(text)) {
            return text;
        }
        Spanned spanned = Html.fromHtml(text);
        if (spanned != null) {
            text = spanned.toString();
            return text.replaceAll("￼", "");
        }
        return null;
    }

    public class DetailData {
        public int id;
        public String createTime;
        public String title;
        public String content;
        public String tag;
        public String audioUrl;
    }

}
